<%@ page contentType="text/html; charset=UTF-8"%>
<%@ include file="/WEB-INF/view/system/common/headLogin.jsp"%>

<body>
	<div id="p" class="easyui-panel" title="修改角色" style="padding: 10px; background: #fafafa;text-align:center;">
		<form id="roleUpdateForm" name="roleUpdateForm" method="post">
			<input type="hidden" id="jsonflag" name="jsonflag" value="validation" />
			<fieldset>
				<legend style="color: blue;font-size:1.5" >
					角色修改
				</legend>
				<table class="infoTable">
					<tr>
						<td  style="width:25%;" >
							角色名称
							<span class="colrRed"> * </span>：
						</td>
						<td>
							<input type="hidden" name="id" value="${role.id}" />
							<input type="text" id="roleName" name="name"
								value="${role.name}" validType="length[1,30]" maxlength="30" class="easyui-validatebox myinput"
								 required="true"  />
						</td>
					</tr>
					<tr>
						<td style="width:25%;" >
							备注 :
						</td>
						<td>
							<textarea class="easyui-validatebox" id="memo"
								 name="descr"
								rows="3" cols="50" >${role.descr}</textarea>
						
							<input type="hidden" id="selectedMenuIds" name="updateRoleMenuIds" />
						</td>
					</tr>
				</table>
			</fieldset>
		
			<table id="test"  style="margin-top:10px;"></table>
			<div style="text-align:center;padding-top:10px;">
			
				<a class="easyui-linkbutton myLink" iconCls="icon-ok"
					onclick="fileupload()">更新 </a>
				&nbsp;&nbsp;&nbsp;
				<a class="easyui-linkbutton myLink" iconCls="icon-undo"
					onclick="resetForm()">重置 </a>
			</div>
		</form>
	</div>
</body>
<script>
//重置
function resetForm() {
	$("#roleUpdateForm").form("reset");
}

$(function() {
	setTreeGridList();
});

var oldMenuIds = ',${oldMenuIds},';
function setTreeGridList() {
	$('#test').treegrid({
		title:'权限树',
		nowrap:false,hoverRowBackground:true,fitColumns:true,lines:true,rownumbers:true,singleSelect:false,fixed:true,animate:true,
		url:'menu/getMenuTreeList.action',
		idField:'id',
		treeField:'name',
		onLoadSuccess:function(){
			var selectedIds='${oldMenuIds}'.split(",");
			for(var i=0;i<selectedIds.length;i++){
				$(this).datagrid('selectRow',selectedIds[i]);
			}
		},
		columns:[[
				{field:'id',title:'ID',hidden:true},
				{field:'name',title:'名称',width:'80%',fix:true,
					formatter:function(value,rowData){
						if(rowData.level==4||rowData.level==5){
							var showValue = "";
							if(oldMenuIds.indexOf(','+rowData.id+',')!=-1){
								showValue+='<input name="menuCheckBox" type="checkbox" checked="true" id="menuCB' + rowData.id + '" value="' + rowData.id + '" />';
							}else{
								showValue+='<input name="menuCheckBox" type="checkbox" id="menuCB' + rowData.id + '" value="' + rowData.id + '" />';
							}
							if(rowData.type==1){
								return showValue+'<span style="color:blue">'+value+'</span>';
							}else {
								return showValue+'<span style="color:red">按钮:'+value+'</span>';
							}
						}
						return '<span style="color:gary">' + value + '</span>';
					}
				}, {field : 'descr',title : '权限描述',fix : true,width : '15%'
				} ] ],
		onSelect : function(row) {
			if (row.level < 4) {
				$('#test').treegrid('unselect', row.id);
			} else {
				if (!document.getElementById("menuCB" + row.id).checked) {
					document.getElementById("menuCB" + row.id).checked = true;
				}
			}
		},
		onUnselect : function(row) {
			if (document.getElementById("menuCB" + row.id).checked) {
				document.getElementById("menuCB" + row.id).checked = false;
			}
		}
	});
}
//页面提交
function fileupload() {
	//判断参数是否为空
	var valid = $('#roleUpdateForm').form('validate');
	if (valid) {
		getSelectMenus();
		if ($("#selectedMenuIds").val().length < 1) {
			$.messager.alert("提示信息", "请选择该角色的权限", 'error');
			return;
		}
		//判断参数是否为空
		var valid = $('#roleUpdateForm').form('validate');
		if (valid) {
			var param = $('#roleUpdateForm').serializeJson(true);
			
			//提交后台处理  
		//	$.post("doRoleUpdate.action" + param, callbackUpdate);
			$.axs("role/doRoleUpdate.action", param, function(data){
				if(data.status == 1){
					$.messager.alert("提示信息",data.msg, 'info', function() {
						parent.$("#roleDataGrid").datagrid('reload');
					
					});
				}else{
					$.messager.alert("提示信息", data.msg, 'error');
				}
			});
		}
	}
}


function getSelectMenus() {
	var selectMenuIds = "";
	var cks = document.getElementsByName("menuCheckBox");
	for ( var i = 0; i < cks.length; i++) {
		if (cks[i].checked == true) {
			selectMenuIds += cks[i].value + ",";
		}
	}
	selectMenuIds = selectMenuIds.substring(0, selectMenuIds.length - 1);
	$("#selectedMenuIds").val(selectMenuIds);
}
</script>
</html>